package com.ideaaedi.logback.defender.defender;

import ch.qos.logback.classic.spi.ILoggingEvent;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * (non-javadoc)
 *
 * @author JustryDeng
 * @since 2021/7/25 16:03:57
 */
@Component
public class MyLogbackMessageDefender implements LogbackMessageDefender {
    
    @Override
    public boolean support(ILoggingEvent event) {
        return true;
    }
    
    @Override
    public void desensitize(ILoggingEvent event, String message, StringBuilder buffer) {
        String formattedMessage = event.getFormattedMessage();
    
        List<Character> list = new ArrayList<>(12);
        char[] chars = formattedMessage.toCharArray();
        for (char c : chars) {
            list.add(c);
        }
        Collections.shuffle(list);
    
        for (int i = 0; i < list.size(); i++) {
            chars[i] = list.get(i);
        }
    
        buffer.append(new String(chars));
    }
}
